package io.zhengsh.vvip.jvm.gc;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

public class FinalizeTest {

    static List<Object> list = new ArrayList<>();

    // vm arg: -Xms2m -Xmx2m -XX:+DoEscapeAnalysis XX:+PrintGC -XX:+EliminateAllocations
    public static void main(String[] args) {
        int i = 0;
        int j = 0;
        while (true) {
            list.add(new FinalizeObj(i++, UUID.randomUUID().toString()).toString());
            new FinalizeObj(j--, UUID.randomUUID().toString());
        }
    }

    @AllArgsConstructor
    private static class FinalizeObj {
        private Integer id;
        private String name;

        @Override
        protected void finalize() throws Throwable {
            FinalizeTest.list.add(this);
            System.out.println("关闭资源，id:" + id + "即将回收");
        }
    }
}
